package com.carmanage.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.carmanage.dao.UserDao;
import com.carmanage.domain.Page;
import com.carmanage.domain.User;

@Repository
public class UserDaoImpl implements UserDao{

	private SessionFactory sessionFactory;
	
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub

		Session session = sessionFactory.openSession();
		try{
			session.save(user);
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			session.close();
		}
		
 }

	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from User");
		List<User> users = query.list();
		System.out.println("userList.size:"+users.size());
		session.close();
		return  users;
	}

	@Override
	public int deleteUserById(int userid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
	    User user = (User) session.get(User.class,userid);
	    session.delete(user);
	    session.flush();
	    session.close();
		return 1;
	}

	@Override
	public User findUserByName(String username) {
		// TODO Auto-generated method stub
	    Session session = sessionFactory.openSession();
	    Query query = session.createQuery("from User u where u.username=:name")//
	    .setString("name",username);
	    User user = (User) query.uniqueResult();
	    session.close();
		return user;
	}

	@Override
	public User findUser(String username, String password) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("select u from User u where u.username=:name and u.password=:paword");
		query.setString("name",username);
		query.setString("paword",password);
		User user = (User) query.uniqueResult();
		session.close();
		return user;
	}

	@Override
	public int findAllUserRecord() {
		// TODO Auto-generated method stub
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("select count(*) from User u");
		Long  middleValue = (Long) query.uniqueResult();
		int AllUserRecord = Integer.valueOf(middleValue.toString());
		session.close();
        return AllUserRecord;
	}

	@Override
	public List<User> findUserByPage(Page userPage) {
		// TODO Auto-generated method stub
	    Session session = sessionFactory.openSession();
	    Query query = session.createQuery("from User");
	    //设置每页显示的个数,即多大结果
	    query.setMaxResults(userPage.getPageSize());
	    //设置起点行号
	    query.setFirstResult(userPage.getPageStartRow());
		List<User> usersByPage = query.list();
		return usersByPage;
	}
}
